import { message } from "@/utils/message";
import { queryExamLoggerList } from "@/api/exam";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useRole() {
  const searchForm = reactive({
    event: "",
    content: "",
    username: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      minWidth: 30,
      type: "index",
      index: (index: number) => {
        return (pagination.currentPage - 1) * pagination.pageSize + (index + 1);
      }
    },
    {
      label: "操作类型",
      prop: "event",
      minWidth: 60
    },
    {
      label: "操作内容",
      prop: "content",
      minWidth: 150,
      cellRenderer: ({ row }) => (
        <el-popover effect="light" trigger="hover" placement="left" width="300">
          {{
            default: () => {
              return (
                <>
                  <el-card>
                    <div v-html={row.content} />
                  </el-card>
                </>
              );
            },
            reference: () => {
              // 去掉 HTML标签
              let result = row.content;
              //end
              const size = result.length;
              if (size > 40) {
                result = result.slice(0, 40) + "...";
              }
              return <div v-html={result} />;
            }
          }}
        </el-popover>
      )
    },
    {
      label: "操作人",
      prop: "creater",
      minWidth: 60
    },
    {
      label: "操作时间",
      prop: "addtime",
      minWidth: 100
    }
  ];

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    queryExamLoggerList({
      event: searchForm.event,
      content: searchForm.content,
      username: searchForm.username,
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    })
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    searchForm,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
